var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

// 页面跳转
var fs = require('fs');
router.get('/form', function(req, res, next){
  var form = fs.readFileSync('./public/form.html', {encoding: 'utf8'});
  res.send(form);
});




// 导入文件上传模块
var multer = require("multer");
var storage = multer.diskStorage({
    destination:function(req,file,cb){
        cb(null,'./public/img');
    },
    filename:function(req,file,cb){
      if(!req.body.arr){
        req.body.arr = [];
      }
      var suffix = file.mimetype.split("/")[1];
      var namr = file.fieldname + '-'+Date.now()+'.'+suffix;
      cb(null,namr);
      req.body.arr.push("/img/"+namr);
    }
});
var upload = multer({storage:storage});




// 添加日记(带照片)
router.post("/upload",upload.array("day"),(req,res)=>{
    console.log(req.body);
    res.json({arr:req.body.arr})
});



module.exports = router;
